/****

    activequant - activestocks.eu

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

	
	contact  : contact@activestocks.eu
    homepage : http://www.activestocks.eu

****/
package org.activequant.workflow.logic.operations;

import java.lang.reflect.Method;

import org.activequant.util.exceptions.IllegalConfigurationException;
import org.activequant.util.exceptions.NotImplementedException;
import org.activequant.workflow.logic.annotations.Condition;
import org.apache.log4j.Logger;


/**
 * An OperationBase subclass. Holds the following associated variables:
 * <ul>
 * <li>methods(Method[])</li>
 * <li>service(Object)</li>
 * <li>target(IOperation)</li>
 * </ul>
 * <br>
 * <b>History:</b><br>
 *  - [07.03.2007] Created (Erik Nijkamp)<br>
 *
 *  @author Erik Nijkamp
 */
public class Jump extends OperationBase {
	
	protected final static Logger log = Logger.getLogger(Jump.class);
	/**
	 * private Method[] methods;
	 */
	private Method[] methods;
	/**
	 * private Object service;
	 */
	private Object service;
	/**
	 * private IOperation target;
	 */
	private IOperation target;
	/**
	 * constructs a Jump(OperationBase subclass) using the given service(Object) and target(IOperation) to set its associated service(Object) and target(IOperation).<br/>
	 * Sets its associated methods(Method[]) with all Condition annotated methods declared in the Class of the given service(Object).
	 * @param service
	 * @param target
	 */
	public Jump(Object service, IOperation target) {
		this.methods = getMethodsByAnnotation(service.getClass(), Condition.class);
		this.service = service;
		this.target = target;
	}
	/**
	 * constructs a Jump(OperationBase subclass) using the given service(Object) and target(IOperation) to set its associated service(Object) and target(IOperation).<br/>
	 * Sets its associated methods(Method[]) with the methods whose name matches the given function(String) declared in the Class of the given service(Object).
	 * @param service
	 * @param function
	 * @param target
	 */
	public Jump(Object service, String function, IOperation target) {
		methods = getMethodsByName(target, function);
		this.service = service;
		this.target = target;
	}
	/**
	 * throws an IllegalConfigurationException("A workflow cannot start with a jump.");
	 */
	public Result execute() throws Exception {
		throw new IllegalConfigurationException("A workflow cannot start with a jump.");
	}
	/**
	 * finds the Method whose signature accepts the given arg(Object) from the associated methods(Method[]) and then invokes that Method on the
	 * associated service(Object) passing in the given arg(Object). Returns a Result object holding the given arg(Object) as its data(Object) and sometimes the associated target(IOperation)
	 * as its operation(IOperation).
	 */
	public Result execute(Object arg) throws Exception {
		Method method = getMethodBySignature(methods, new Object[]{arg});
		log.info("Invoking method '" + method.getName()
				+ "' in service class '" + service.getClass().getSimpleName()
				+ "'.");
		Boolean condition = (Boolean) method.invoke(service, new Object[]{arg});
		// Use given arguments for the following step
		if(condition) {
			return new Result(arg);
		} else {
			return new Result(arg, target);
		}
	}
	/**
	 * returns the associated methods(Method[])
	 */
	public Method[] getMethods() {
		return methods;
	}
	/**
	 * throws a NotImplementedException("Invalid use of this operation.")
	 */
	public Result execute(Class<?> signature) throws Exception {
		throw new NotImplementedException("Invalid use of this operation.");
	}
}
